<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
      style="width: 100%;" >
      <el-table-column type="selection" header-align="center" align="center" width="50">
      </el-table-column>
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="创建时间">
      </el-table-column>
      <el-table-column prop="imageCode" header-align="center" align="center" label="ImageCode">
      </el-table-column>
      <el-table-column prop="orderCode" header-align="center" align="center" label="订单编号">
      </el-table-column>
      <el-table-column prop="imageWidth" header-align="center" align="center" label="宽(px)">
      </el-table-column>
      <el-table-column prop="imageHeight" header-align="center" align="center" label="高(px)">
      </el-table-column>
      <el-table-column prop="imageGenPrompt" header-align="center" align="center" label="提示词" min-width="200">
        <template slot-scope="scope">
          <div class="prompt-container">
            {{ scope.row.imageGenPrompt }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="imageGenSourceImgUrl" header-align="center" align="center" label="生成图">
        <template slot-scope="scope">
          <div class="image-container">
            <img :src="scope.row.imageGenSourceImgUrl" class="preview-image" @click="imagePreviewHandle(scope.row.imageGenSourceImgUrl)"/>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="imageGenMaterialCode" header-align="center" align="center" label="素材Code">
      </el-table-column>
      <el-table-column prop="imageGenTemplateCode" header-align="center" align="center" label="模板Code">
      </el-table-column>
      <el-table-column prop="creator" header-align="center" align="center" label="创建者">
      </el-table-column>
      <el-table-column prop="modifier" header-align="center" align="center" label="修改者">
      </el-table-column>
      <el-table-column prop="userCode" header-align="center" align="center" label="用户编号">
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <!-- 查看大图 -->
    <ImagePreview v-if="imagePreviewVisible" ref="imagePreview"  @refreshDataList="getDataList"></ImagePreview>
 </div>
</template>

<script>
import AddOrUpdate from './wmhGalaxyImage-add-or-update'
import ImagePreview from '@/views/common/image-preview'
export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      score:0,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      imagePreviewVisible: false
    }
  },
  components: {
    AddOrUpdate,
    ImagePreview
  },
  activated() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/image/image/queryWormholeGalaxyImageList'),
        method: 'post',
        data: this.$http.adornData({
          isPage: true,
          pageNumb: this.pageIndex,
          pageSize: this.pageSize,
        })
      }).then(({ data }) => {
        if (data && data.success) {
          this.dataList = data.data
          /**
           * TODO
           */
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 查看大图
    imagePreviewHandle(url){
      this.imagePreviewVisible = true
         this.$nextTick(() => {
        this.$refs.imagePreview.init(url)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/image/image/deleteWormholeGalaxyImage'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>

<style scoped>
.prompt-container {
  max-height: 100px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-wrap: break-word;
  padding: 5px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}

.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 120px;
  padding: 10px 0;
}

.preview-image {
  max-width: 100px;
  max-height: 100px;
  cursor: pointer;
  object-fit: contain;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 5px;
  display: block;
}

.preview-image:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}
</style>
